export default {
    name: "Exceptionreport",
    components: {},
    data() {
        return {
            height: '600',
            total: 0,
            currentPage: 1,
            pagesize: 5,
            timeInterval: [this.NowTimestamp((new Date()).getTime() - 4665600000), this.NowTimestamp((new Date()).getTime())],
            exceptionData: [],
            multipleSelection: [],
            users: "",
            abnormalTotal:"",
            handledException:"",
            unhandledException:"",
        };
    },
    computed: {
        offset: function () {
            return (this.currentPage - 1) * this.pagesize;
        }
    },
    watch: {},
    created() { 
        this.users = JSON.parse(window.localStorage.getItem("userInfo"));
        this.initExceptionCount();
        this.initExceptionlist();
    },
    mounted() { },
    methods: {
        NowTimestamp(timestamp) {
            var date = new Date(timestamp); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
            var Y = date.getFullYear() + '-';
            var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
            var D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate() + ' ');
            var h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
            var m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
            var s = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
            return Y + M + D;
        },
        //异常统计
        initExceptionCount(){
          this.$http.get('/alarmRecord/abnormalStatistics?userId='+this.users.uId)
           .then((res) => {
             if(res.status == 200){
               this.abnormalTotal = res.data.abnormalTotal;
               this.handledException = res.data.handledException;
               this.unhandledException = res.data.unhandledException;
             }
           }).catch((err) => {
               console.log(err);
           });
        },
        unreadFormatter(row, cloumn, cellValue, index){
            return cellValue ? '未读' : '已读';
        },
        //异常报表查询
        initExceptionlist() {
           this.$http.get("/alarmRecord/search?limit="+this.pagesize+"&offset="+this.offset + "&userId="+this.users.uId)
            .then((res) => {
              if(res.status == 200){
                  this.exceptionData = res.data;
                  this.total = res.total;
              }
            }).catch((err) => {
             console.log(err);
          });
        },
        //导出excel表格
        exportClick() {
            //导出修正文件名--利用a标签
            if (this.multipleSelection.length < 1) {
                this.$message.error("请选择要导出的异常信息");
                return false;
            } else{
                var faultdIds = [];
                this.multipleSelection.forEach(function(val,idx){
                    faultdIds.push(val.faultId);
                });
                var txtName = faultdIds.join(",");
                this.$http.post('/alarmRecord/alarmRecordExport', this.qs.stringify({
                  faultIds : txtName
                }), {
                    responseType: 'arraybuffer'
                }).then((res) => {
                   const content = res;
                   const blob = new Blob([content], {
                   type: "application/vnd.ms-excel"
                    })
                    const fileName = '异常报表.xls';
                    if('download' in document.createElement('a')){
                        //非IE下载
                        const elink = document.createElement("a");
                        elink.download = fileName;
                        elink.style.display = "none";
                        elink.href = URL.createObjectURL(blob);
                        document.body.appendChild(elink);
                        elink.click();
                        URL.revokeObjectURL(elink.href);//释放URL对象
                        document.body.removeChild(elink);
                        this.initExceptionlist();
                      } else {
                        //IE10下载
                        navigator.msSaveBlob(blob,fileName);
                     }
                 }).catch((err) => {
                  console.log(err);
              });
            }
        },
        toggleSelection(rows) {
            if (rows) {
                rows.forEach(row => {
                    this.$refs.multipleTable.toggleSelection(row);
                });
            } else {
                this.$refs.multipleTable.clearSelection();
            }
        },
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        handleSizeChange(val) {
            this.pagesize = val;
            this.initExceptionlist();
        },
        handleCurrentChange(val) {
            this.currentPage = val;
            this.initExceptionlist();
        }
    }
};